package cn.edu.nuc.zyb.frame;

import java.awt.EventQueue; 

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.zyb.client.UserClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class UserLogin extends JFrame {

	private JPanel contentPane;
	private JTextField usernameField;
	private JPasswordField passwordField;
	public List<Socket> list = new ArrayList<>();
	public boolean flag = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserLogin frame = new UserLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserLogin() {
		setTitle("中北在线商场-登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);

		usernameField = new JTextField();
		usernameField.setBackground(Color.GRAY);
		usernameField.setBounds(151, 146, 197, 21);
		contentPane.add(usernameField);
		usernameField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBackground(Color.GRAY);
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);

		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String username = usernameField.getText();
				String password = passwordField.getText();

				
				int password1 = Integer.valueOf(password);

				UserClient uc = new UserClient();
				boolean flag = uc.sendMsg(username, password1);
				
				// System.out.println(uc.acceptMsg());
				// boolean flag =uc.acceptMsg();
				if (flag) {
					UserStore store = new UserStore(UserLogin.this,username);
					JOptionPane.showMessageDialog(null, "登陆成功");
					store.setVisible(true);
					UserLogin.this.setVisible(false);
				} else {
					JOptionPane.showMessageDialog(null, "登陆失败");
				}

			}
		});
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("E:\\Javaworkspace\\Online_zyb\\image\\a.jpg"));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);

		JButton button_1 = new JButton("注册");
		button_1.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				UserRegist ur = new UserRegist();
				ur.setVisible(true);
			}
		});
		button_1.setBounds(151, 216, 93, 24);
		contentPane.add(button_1);
	}
}
